<template>
    <div class="fTable">
        <table v-loading="loading" element-loading-text="数据加载中">
            <thead>
                <tr>
                    <th v-for="(column,index) in columns" :key="index" :style="{width:column.width+'px'}">{{column.title}}
                        <div class="th-title" v-if="column.switch">
                            <a class="switchBar pull-right" @click="onOff($event)">
                                <span class="switch-inner" :class="[{'active':column.switch.onActive},{'pointer':!column.switch.onActive}]">{{column.switch.onText}}</span>
                                <span class="switch-inner" :class="[{'active':!column.switch.onActive},{'pointer':column.switch.onActive}]">{{column.switch.offText}}</span>
                                <span :class="[{'leftBar':column.switch.onActive},{'rightBar':!column.switch.onActive}]" id="Bar"></span>
                            </a>
                        </div>
                        <div class="th-button pointer" v-if="column.button" @click="thClick(column.button.filed)">
                            <el-tooltip class="item" effect="dark" :content="column.button.tooltip" placement="top">
                                <span class="iconfont" :class="column.button.icon"></span>
                            </el-tooltip>
                        </div>
                    </th>
                </tr>
            </thead>
            <tbody>
                <tr v-for="(row,index) in datas" :key="index">
                    <slot :row="row" :index="index"></slot>
                </tr>
            </tbody>
        </table>
    </div>
</template>
<script>
export default {
    props: ['columns', 'datas', 'loading'],
    data() {
        return {
            flag: true
        };
    },
    methods: {
        onOff(ev) {
            if (this.flag) {
                ev.currentTarget.children[2].className = 'rightBar';
                ev.currentTarget.children[0].className = 'switch-inner pointer';
                ev.currentTarget.children[1].className = 'switch-inner active ';
            } else {
                ev.currentTarget.children[2].className = 'leftBar';
                ev.currentTarget.children[1].className = 'switch-inner pointer';
                ev.currentTarget.children[0].className = 'switch-inner active ';
            }
            this.flag = !this.flag;
            this.$emit('switchChange', this.flag);
        },
        thClick(value) {
            this.$emit('activeColumn', value);
        }
    }
};
</script>
<style lang="scss">
@import "../../assets/css/common.scss";
.fTable {
  width: 100%;
  table {
    width: 100%;
    border: 1px solid $BORDER_COLOR;
    border-collapse: collapse;
    thead {
      background: $BG_GRAY;
      th {
        border-bottom: 1px solid $BORDER_COLOR;
        padding-left: 15px;
        padding-right: 15px;
        text-align: left;
        color: $MAIN_GRAY_COLOR;
      }
    }
    tr {
      height: 60px;
      &:last-child {
        td {
          border: none;
        }
      }
      td {
        padding-left: 15px;
        padding-right: 15px;
        border-bottom: 1px solid $BORDER_COLOR;
      }
    }
    .th-title {
      display: inline-block;
      vertical-align: top;
      .switchBar {
        display: inline-flex;
        width: 80px;
        position: relative;
        height: 22px;
        background: $MAIN_BLUE_COLOR;
        border-radius: 20px;
        margin-right: 20px;
        .switch-inner {
          display: inline-block;
          width: 40px;
          line-height: 22px;
          text-align: center;
          font-size: $FONT_SIZE - 2;
          height: 25px;
          color: $WHITE_COLOR;
          z-index: 999;
        }
        .active {
          transition: left 1s;
          color: $BLACK_FONT_COLOR;
        }
        #Bar {
          position: absolute;
          top: 1.5px;
          background: $WHITE_COLOR;
          height: 18px;
          border-radius: 20px;
          width: 38px;
        }
        .leftBar {
          transition: left 0.5s;
          left: 2.5px;
        }
        .rightBar {
          left: 39px;
          transition: left 0.5s;
        }
      }
    }
    .th-button {
      display: inline-block;
      vertical-align: top;
    }
  }
}
</style>
